-- 上个雕像
local _DoRecipeClick = GLOBAL.DoRecipeClick
local last_cp = "chesspiece_knight_builder"
local last_skin     -- 万一以后科雷给雕像出皮肤了呢
GLOBAL.DoRecipeClick = function(owner, recipe, skin, ...)
    local str = recipe and recipe.product or ""
    if string.sub(str, 1, 11) == "chesspiece_" then
        last_cp = str
        last_skin = skin
    end
    return _DoRecipeClick(owner, recipe, skin, ...)
end

local e_util = require "libs/entutil"
local p_util = require "libs/playerutil"

local thread
local id_thread = "HUXI_AUTO_CHESSPIECE"
local function TIPS(color, content)
    local name = GLOBAL.STRINGS.NAMES[last_cp:upper()]
    TIP((name or "上个雕像"), color, content)
end
local function StopThread(message, color)
    GLOBAL.KillThreadsWithID(id_thread)
    thread = nil
    TIPS("red", message or "制作终止")
end
-- p_util:GetSlotFromAll("cutstone")
local stones = {"marble", "cutstone", "moonglass"}
local function fn()
    if thread then
        return StopThread()
    end
    TIPS("green", "开始")
    thread = GLOBAL.StartThread(function()
        while thread do
            -- 有雕像丢雕像
            local heavy = p_util:GetItemFromAll(nil,"heavy",nil,"equip")
            if heavy then p_util:DropItemFromInvTile(heavy) else
                -- 没陶轮可不行
                local sculptingtable = e_util:FindEnt(nil, "sculptingtable", 40, nil, {'FX','DECOR','INLIMBO','NOCLICK', 'player', 'fire', 'burnt'})
                local anim = sculptingtable and sculptingtable.AnimState
                if not anim then StopThread("未找到陶轮") break else 
                    -- 有雕像则拿
                    local has_heavy = anim:GetSymbolOverride("swap_body")
                    if has_heavy then
                        if p_util:GetActiveItem() then p_util:ReturnActiveItem() else p_util:ActClick(sculptingtable, "PICK", true) end
                    else
                        -- 没雕像看看上面有无材料
                        local has_stone = anim:GetSymbolOverride("cutstone01")
                        if has_stone then
                            -- 能否制作
                            if not p_util:CanBuild(last_cp)then 
                                local dist = e_util:GetDist(sculptingtable)
                                if dist and dist<4 then StopThread("我做不到") else StopThread("距离陶轮过远") end 
                                break
                            else p_util:MakeSth(last_cp, last_skin) end
                        else
                            -- 有无手里剑
                            local mouse = p_util:GetActiveItem()
                            if mouse then
                                if table.contains(stones, mouse.prefab) then
                                    p_util:ActClick(sculptingtable, "GIVE")
                                else p_util:ReturnActiveItem() end
                            else
                                local stone = p_util:GetSlotFromAll(stones)
                                if stone then
                                    local cont = e_util:GetContainer(stone.container)
                                    if cont then
                                        cont:TakeActiveItemFromAllOfSlot(stone.slot)
                                    else
                                        StopThread("发生意外中断！")
                                        break
                                    end
                                else
                                    local cant = true
                                    for _, stone in ipairs(stones)do
                                        if p_util:CanBuild(stone)then
                                            p_util:MakeSth(stone)
                                            cant = false
                                        end
                                    end
                                    if cant then StopThread("不能制作材料") break end
                                end
                            end
                        end
                    end
                end
            end
            repeat
                GLOBAL.Sleep(GLOBAL.FRAMES*3)
            until not p_util:IsInBusy()
        end
    end)
end

InterruptedByMobile(function() return thread end, StopThread)
if GetModConfigData("sw_shadowheart") == "biubiu" then DEAR_BTNS:AddDearBtn(GLOBAL.GetInventoryItemAtlas("sculptingtable.tex"), "sculptingtable.tex", "上个雕像", "重复制作并丢弃上个制作过的雕像【原黑心工厂】", true, fn)end
AddBindBtn("sw_shadowheart", fn)